<?php
// +--------------------------------------------------------------------------
// | ZAIYOUDAO [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2016 载攸道 版权所有 <TEL:17090888854>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * 系统导航
 */
namespace CMS\Controller;
use Common\Controller\AdminController;
use Think\Upload;

/**
 * 导航控制器
 */
class ImportController extends AdminController{
	
	/**
	 * 导航信息
	 */
    public function Manage(){
		
		action_log('admin_nav_manage');//行为记录
		
		common_public()->assign_page('导航管理');
		
		$m_common_import = M('common_import');
		
		//获得上级信息
		$fmap['id'] = I('param.form_id')?I('param.form_id'):0;
		$data = $m_common_import->where($fmap)->find();
		
		if(!$data){
			
			$type = 2;
		}else{
			$type = 1;
			
		}
		
		//获得菜单信息
		$map['form_id'] = $fmap['id'];
		$map['status'] = 1;
		
		$list = $m_common_import->where($map)->order('sort desc')->select();
		
		//赋值
		
		$this->assign('list',$list);
		$this->assign('data',$data);
		$this->assign('type',$type);
		$this->assign('form_id',$fmap['id']);
		
		
		$this->display();
	}
	
	/**
	 * 新增导航
	 */
	public function Create(){
		
		action_log('admin_nav_create');//行为记录
		
		//判断是否是ajax请求
		if(IS_AJAX){

			$m_common_import = M('common_import');
		
			//获得数据
			$data = I();
			
			$data['uid'] = getUID();
			$data['time'] = time();
			$data['ip'] = get_client_ip();
			$data['status'] = 1;
			
			$result = $m_common_import->add($data);
			
			//返回信息
			$info = $result?get_errcode(0):get_errcode(-1);
				
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);
	}
	
	/**
	 * 编辑导航
	 */
	public function Edit(){
    
		action_log('admin_nav_edit');//行为记录
		
		//判断是否是ajax请求
		if(IS_AJAX){

			$m_common_import = M('common_import');
		
			$map['id'] = I('param.id');
			
			$data = I('');
			unset($data['id']);
		
			$result = $m_common_import->where($map)->save($data);
			
			//返回信息
			$info = $result?get_errcode(0):get_errcode(-1);
		
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);
	}
	
	/**
	 * 删除导航
	 */
	public function Delete($id,$status=0){
		
		action_log('admin_nav_delete');//行为记录
		
		//判断是否是ajax请求
		if(IS_AJAX){
				
			$m_common_import = M('common_import');
		
			$id = $id?$id:I('param.id');
			$status = $status?$status:I('param.status');
			
			if($id){
				
			
				$id = explode(',',$id); 
				
				
				foreach($id as $i=>$k){
					
					$map['form_id'] = $id[$i];
					
					$m_common_import->delete($id[$i]);
					$result = $m_common_import->where($map)->delete();
					
					
				}
				
				//返回信息
				$info = get_errcode(0);
			
			}else{
				
				$info = get_errcode(10000001);
			}
			
	
		
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);


	}
	
	
		public function  Import(){
			
			if(IS_POST){
			
			
	
			$upload = new Upload();// 实例化上传类    
			$upload->maxSize   =     3145728 ;// 设置附件上传大小    
			$upload->exts      =     array('xls','xlsx');// 设置附件上传类型    
			$upload->savePath  =      './ISNH5/'; // 设置附件上传目录    // 上传文件     
			$upload->saveName = array('uniqid','');  
			$info   =   $upload->upload(); 
			
			if(!$info) {
				// 上传错误提示错误信息        
				
				 $this->error($upload->getError());
				 
				 
			}else{
				
				//上传成功        
				//要导入的xls文件，位于根目录下的Public文件夹
				$filename= "Uploads/".$info['xls']['savepath'].$info['xls']['savename'];
				//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
				import("Org.Util.PHPExcel");
				//创建PHPExcel对象，注意，不能少了\
				$PHPExcel=new \PHPExcel();
				//如果excel文件后缀名为.xls，导入这个类
				import("Org.Util.PHPExcel.Reader.Excel5");
				$PHPReader=new \PHPExcel_Reader_Excel5();
				//载入文件
				$PHPExcel=$PHPReader->load($filename);
				//获取表中的第一个工作表，如果要获取第二个，把0改为1，依次类推
				$currentSheet=$PHPExcel->getSheet(0);
				//获取总列数
				$allColumn=$currentSheet->getHighestColumn();
				//获取总行数
				$allRow=$currentSheet->getHighestRow();
				//循环获取表中的数据，$currentRow表示当前行，从哪行开始读取数据，索引值从0开始
				for($currentRow=2;$currentRow<=$allRow;$currentRow++){
					//从哪列开始，A表示第一列
					for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){
						//数据坐标
						$address=$currentColumn.$currentRow;
						//读取到的数据，保存到数组$arr中
						$arr[$currentRow][$currentColumn]=$currentSheet->getCell($address)->getValue();
					}
				
				}
	
				
				$m_common_import = M('common_import');
				
				$map['id'] = I('param.id');
				$data = $m_common_import->where($map)->find();
				
				$map_down['form_id'] = I('param.id');
				$map_down['status'] = 1;
				
				$list_info = $m_common_import->where($map_down)->select();
				
				
				$m_model = M($data['name']);
				
				
			
				foreach($arr as $list){
				
					foreach($list_info as $i=>$k){
						
						
						$datas[$list_info[$i]['value']] = $list[$list_info[$i]['name']];
						
					}
					
					
					$m_model->add($datas);
	
					
				}
			
				
				$this->success('处理成功');
				
			}
			
			}else{
				
				$id = I('param.id');
				
				$this->assign('id',$id);
		
				$this->display();
			}
	}
	
	
	/**
	 * 导航信息
	 */
    public function Export(){
		
		action_log('admin_nav_manage');//行为记录
		
		
		$m_common_import = M('common_import');
		
		//获得上级信息
		$id = I('param.id');
		
		$map['id'] = I('param.id');
		$data = $m_common_import->where($map)->find();
		
		$map_down['form_id'] = I('param.id');
		$map_down['status'] = 1;
		
		$list_info = $m_common_import->where($map_down)->select();
		
		$m_model = M($data['name']);
		
		//上传开始结束时间
		$begin_id = I('param.begin_id');
		$end_id = I('param.end_id');
		if($begin_id != null and $end_id != null){
			
			$maps['id']  = array(array('egt',$begin_id),array('elt',$end_id),'and'); 
			
		}else{
			
			//默认今天明天
			$maps['id'] = 0;
		}
		$this->assign('begin_id',$begin_id);
		$this->assign('end_id',$end_id);
		
		
		$list = $m_model->where($maps)->select();
		
		foreach($list as $i=>$k){
			
			
			
			foreach($list_info as $ii=>$ik){
				
				if($ii==0){
					
					$list[$i]['info'] = '<td>'.$list[$i][$list_info[$ii]['value']].'</td>';
				
					
				}else{
					
					$list[$i]['info'] = $list[$i]['info'].'<td>'.$list[$i][$list_info[$ii]['value']].'</td>';
				
				}
				
				
			}
			
		}
		
		
		$this->assign('id',$id);
		$this->assign('list',$list);
		$this->assign('list_info',$list_info);
		
		
		$this->display();
	}
}